<?php
include 'db.php';
header("Content-type: application/json");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Origin: *");
header("Access-Control-Request-Methods:GET, POST, PUT, DELETE, OPTIONS");
header('Access-Control-Allow-Headers:*');

class services
{
    static private $instance;

    private function __construct()
    {
    }

    private function __clone()
    {
    }

    static public function getInstance()
    {
        if (!self::$instance instanceof self) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     *接收头信息
     **/
    static function em_getallheaders()
    {
        foreach ($_SERVER as $name => $value) {
            if (substr($name, 0, 5) == 'HTTP_') {
                $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
            }
        }
        return $headers;
    }



    public static function success($data)
    {
        echo json_encode(array(
            'code' => 200,
            'data' => $data,
            'message' => "",
        ), JSON_UNESCAPED_UNICODE);
    }
    public static function fail($message)
    {
        echo json_encode(array(
            'code' => 100,
            'data' => "",
            'message' => $message,
        ), JSON_UNESCAPED_UNICODE);
    }

    /**
     * 考试科目
     */
    function addsubject()
    {
        if (empty($_POST['name']) || empty($_POST['startime']) || empty($_POST['endtime']))
            exit(self::fail("POST請求數據不合法"));
        $name = $_POST['name'];
        $startime = $_POST['startime'];
        $endtime = $_POST['endtime'];
        $sql = "INSERT INTO `subject` (`name`, `startime`, `endtime`) VALUES ('$name','$startime','$endtime')";
        if (db::_bool($sql) === TRUE) {
            exit(self::success('考試科目添加成功'));
        } else {
            exit(self::fail('考試科目添加失敗,sql:' . $sql));
        }
    }

    function editsubject()
    {
        if (empty($_POST['id']) || empty($_POST['name']) || empty($_POST['startime'] || empty($_POST['endtime'])))
            exit(self::fail("POST請求數據不合法"));
        $id = $_POST['id'];
        $name = $_POST['name'];
        $startime = $_POST['startime'];
        $endtime = $_POST['endtime'];
        $sql = "UPDATE `subject` SET `name`='$name',`startime`='$startime',`endtime`='$endtime' WHERE `id`=$id";
        if (db::_bool($sql) === TRUE) {
            exit(self::success('考試科目更改成功'));
        } else {
            exit(self::fail('考試科目更改失敗,sql:' . $sql));
        }
    }

    function delsubject()
    {
        if (empty($_GET['id']))
            exit(self::fail("GET請求數據不合法"));
        $id = $_GET['id'];
        $sql = "DELETE FROM `subject` WHERE `id` = $id";
        if (db::_bool($sql) === TRUE) {
            exit(self::success('考試科目刪除成功'));
        } else {
            exit(self::fail('考試科目刪除失敗,或存在危险的索引数据删除,sql:' . $sql));
        }
    }

    /**
     * 考生信息
     */
    function addexaminee()
    {
        if (empty($_POST['testnumber']) || empty($_POST['idnumber']) || empty($_POST['name'] || empty($_POST['password'])))
            exit(self::fail("POST請求數據不合法"));
        $testnumber = $_POST['testnumber'];
        $idnumber = $_POST['idnumber'];
        $name = $_POST['name'];
        $password = $_POST['password'];
        $sql = "INSERT INTO `examinee` (`testnumber`, `idnumber`, `name`, `password`) VALUES ('$testnumber','$idnumber','$name','$password')";
        if (db::_bool($sql) === TRUE) {
            exit(self::success('考生信息添加成功'));
        } else {
            exit(self::fail('考生信息添加失敗,sql:' . $sql));
        }
    }

    function editexaminee()
    {
        if (empty($_POST['testnumber']) || empty($_POST['idnumber']) || empty($_POST['name'] || empty($_POST['password'])) || empty($_POST['id']))
            exit(self::fail("POST請求數據不合法"));
        $testnumber = $_POST['testnumber'];
        $idnumber = $_POST['idnumber'];
        $name = $_POST['name'];
        $password = $_POST['password'];
        $id = $_POST['id'];
        $sql = "UPDATE `examinee` SET `testnumber`='$testnumber',`idnumber`='$idnumber',`name`='$name',`password`='$password' WHERE `id`=$id";
        if (db::_bool($sql) === TRUE) {
            exit(self::success('考生信息更改成功'));
        } else {
            exit(self::fail('考生信息更改失敗,sql:' . $sql));
        }
    }

    function delexaminee()
    {
        if (empty($_GET['id']))
            exit(self::fail("GET請求數據不合法"));
        $id = $_GET['id'];
        $sql = "DELETE FROM `examinee` WHERE `id` = $id";
        if (db::_bool($sql) === TRUE) {
            exit(self::success('考生信息刪除成功'));
        } else {
            exit(self::fail('考生信息刪除失敗,或存在危险的索引数据删除,sql:' . $sql));
        }
    }

    /**
     * 文件信息
     */
    function addfile()
    {
        if (empty($_POST['filelocation']) || empty($_POST['addtime']) || empty($_POST['examinee_id']) || empty($_POST['subject_id']))
            exit(self::fail("POST請求數據不合法"));
        $filelocation = $_POST['filelocation'];
        $addtime = $_POST['addtime'];
        $examinee_id = $_POST['examinee_id'];
        $subject_id = $_POST['subject_id'];
        $sql = "INSERT INTO `file` (`filelocation`, `addtime`, `examinee_id`, `subject_id`) VALUES ('$filelocation','$addtime','$examinee_id','$subject_id')";
        if (db::_bool($sql) === TRUE) {
            exit(self::success('作答文件添加成功'));
        } else {
            exit(self::fail('作答文件添加失敗,sql:' . $sql));
        }
    }

    function editfile()
    {
        if (empty($_POST['filelocation']) || empty($_POST['addtime']) || empty($_POST['examinee_id']) || empty($_POST['subject_id']) || empty($_POST['id']))
            exit(self::fail("POST請求數據不合法"));
        $filelocation = $_POST['filelocation'];
        $addtime = $_POST['addtime'];
        $examinee_id = $_POST['examinee_id'];
        $subject_id = $_POST['subject_id'];
        $id = $_POST['id'];
        $sql = "UPDATE `file` SET `filelocation`='$filelocation',`addtime`='$addtime',`examinee_id`='$examinee_id',`subject_id`='$subject_id' WHERE `id`=$id";
        if (db::_bool($sql) === TRUE) {
            exit(self::success('作答文件更改成功'));
        } else {
            exit(self::fail('作答文件失敗,sql:' . $sql));
        }
    }

    function delfile()
    {
        if (empty($_GET['id']))
            exit(self::fail("GET請求數據不合法"));
        $id = $_GET['id'];
        $sql = "DELETE FROM `file` WHERE `id` = $id";
        if (db::_bool($sql) === TRUE) {
            exit(self::success('作答文件刪除成功'));
        } else {
            exit(self::fail('作答文件刪除失敗,sql:' . $sql));
        }
    }

    /**
     * 获取
     */
    function getfile()
    {
        $key = NULL;
        if (!empty($_GET['key']))
            $key = $_GET['key'];
        $sql = "SELECT `id`,`filelocation`, `addtime`,`examinee_id`,`subject_id` FROM `file` " . ($key != null & $key != '' ? 'WHERE id=' . $key : '');
        exit(self::success(db::_array($sql)));
    }

    function getsubject()
    {
        $key = NULL;
        if (!empty($_GET['key']))
            $key = $_GET['key'];
        $sql = "SELECT `id`,`name`, `startime`,`endtime` FROM `subject` " . ($key != null & $key != '' ? 'WHERE id=' . $key : '');
        exit(self::success(db::_array($sql)));
    }

    function getexaminee()
    {
        $key = NULL;
        if (!empty($_GET['key']))
            $key = $_GET['key'];
        $sql = "SELECT `id`,`testnumber`, `idnumber`,`name`,`password` FROM `examinee` " . ($key != null & $key != '' ? 'WHERE id=' . $key : '');
        exit(self::success(db::_array($sql)));
    }

    function getfilewithexamineewithsubject()
    {
        $key = NULL;
        if (!empty($_GET['key']))
            $key = $_GET['key'];
        $sql = "SELECT `id`,`filelocation`, `addtime`,`examinee_id`,`subject_id` FROM `file` " . ($key != null & $key != '' ? 'WHERE id=' . $key : '');
        exit(self::success(db::_array($sql)));
    }

    function login()
    {
        if (empty($_POST['testnumber']) || empty($_POST['password']))
            exit(self::fail('登陆失败'));
        $testnumber = $_POST['testnumber'];
        $password = $_POST['password'];
        $sql = "SELECT `id`,`testnumber`, `idnumber`,`name`,`password` FROM `examinee` WHERE `testnumber`=$testnumber AND `password`= $password";
        if (empty(db::_array($sql)))
            exit(self::fail('登陆失败'));
        exit(self::success(db::_array($sql)));
    }

    function loginadmin()
    {
        if (empty($_POST['name']) || empty($_POST['password']))
            exit(self::fail('登陆失败'));
        $name = $_POST['name'];
        $password = $_POST['password'];
        $sql = "SELECT `id`,`name` FROM `user` WHERE `name`='$name' AND `password`= '$password'";
        if (empty(db::_array($sql)))
            exit(self::fail('登陆失败'));
        exit(self::success(db::_array($sql)));
    }

    function getmyfile()
    {
        $token = self::em_getallheaders()['Token'];
        $sql = "SELECT `id`,`filelocation`, `addtime`,`examinee_id`,`subject_id` FROM `file` WHERE examinee_id='$token'";
        exit(self::success(db::_array($sql)));
    }

    function getmyinfo()
    {
        $token = self::em_getallheaders()['Token'];
        $sql = "SELECT `id`,`testnumber`, `idnumber`,`name`,`password` FROM `examinee` WHERE id='$token'";
        exit(self::success(db::_array($sql)));
    }

    function addmyfile()
    {
        $fileInfo = $_FILES['file']; //获取提交过来的文件
        $filename = $fileInfo['name']; //获取文件名
        $filePath = $fileInfo['tmp_name']; //获取文件临时目录
        $name = iconv('utf-8', 'gb2312', $filename); //把文件名重新编码，避免吗中文乱码
        $dirname = 'uploader/' . $_GET['testnumber'] . '/';
        if (!is_dir($dirname)) {
            mkdir(iconv("UTF-8", "GBK", $dirname), 0777, true);
        }
        $filelocation = $dirname . $name;
        move_uploaded_file($filePath, $dirname . $name);
        exit(self::success($filelocation));
    }

    function addmyfile2()
    {
        if (empty($_POST['testnumber']) || empty($_POST['subject_id'])|| empty($_POST['filelocation']) || empty($_POST['examinee_id']))
            exit(self::fail('添加失败'));
        $filelocation = $_POST['filelocation'];
        $addtime = date("Y/m/d");
        $examinee_id = $_POST['examinee_id'];
        $subject_id = $_POST['subject_id'];
        $sql = "INSERT INTO `file` (`filelocation`, `addtime`, `examinee_id`, `subject_id`) VALUES ('$filelocation','$addtime','$examinee_id','$subject_id')";
        if (db::_bool($sql) === TRUE) {
            exit(self::success('作答文件添加成功'));
        } else {
            exit(self::fail('作答文件添加失敗,sql:' . $sql));
        }
    }
}
